package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.CustomerDepartmentAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CustomerRelationKeyDepartment;
import com.isunland.managesystem.entity.CustomerRelationKeyDepartment.DepartMentList;
import com.isunland.managesystem.utils.LogUtil;
import com.isunland.managesystem.utils.SharedPreferencesUtil;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 客户部门列表
 *
 * @author wu
 */
public class CustomerRelationDepartmentListFragment extends BaseListFragment {

  private String TAG = CustomerRelationDepartmentListFragment.class.getName();
  private CustomerDepartmentAdapter mAdapter;
  HashMap<String, String> mDepartmentList;
  private String customerId;
  DepartMentList departMentList;
  private static final int REQUEST_ADD = 0;
  private static final int REQUEST_ALTER = 1;

  @Override public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setTitleCustom(R.string.customerKeyDepartment);
    customerId =
        SharedPreferencesUtil.getString(getActivity(), SharedPreferencesUtil.CUSTOMER_ID, "");
    Log.i(TAG, customerId);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu_add, menu);
  }

  @Override public HashMap<String, String> getparameters() {
    mDepartmentList = new HashMap<>();
    mDepartmentList.put("curPage", "1");
    mDepartmentList.put("pageSize", "1000");
    mDepartmentList.put("customerId", customerId);
    LogUtil.i(mDepartmentList.toString());
    return mDepartmentList;
  }

  @Override public String getUrl() {

    return ApiConst.URL_CUSTOMER_RELATION_DEPARTMENT;
  }

  @Override public void resolveResponse(String json) {
    CustomerRelationKeyDepartment keyDepartment =
        new Gson().fromJson(json, CustomerRelationKeyDepartment.class);

    ArrayList<DepartMentList> department = keyDepartment.getDataList();

    setAdapter(department);
  }

  private void setAdapter(ArrayList<DepartMentList> keyDepartment) {
    if (keyDepartment == null) {
      keyDepartment = new ArrayList<>();
    }
    mAdapter = new CustomerDepartmentAdapter(mActivity, keyDepartment);
    setListAdapter(mAdapter);
    ((CustomerDepartmentAdapter) getListAdapter()).notifyDataSetChanged();
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    departMentList = mAdapter.getItem(position - 1);
    String pid = mAdapter.getItem(position - 1).getPid();
    String managerId = mAdapter.getItem(position - 1).getManagerId();
    String managerName = mAdapter.getItem(position - 1).getManagerName();
    String directorId = mAdapter.getItem(position - 1).getDirectorId();
    String directorName = mAdapter.getItem(position - 1).getDirectorName();
    String departmentName = mAdapter.getItem(position - 1).getOrgName();
    String departmentId = mAdapter.getItem(position - 1).getId();
    String dataStatus = mAdapter.getItem(position - 1).getDataStatus();
    String customerID = mAdapter.getItem(position - 1).getCustomerId();
    String customerName = mAdapter.getItem(position - 1).getCustomerName();
    String departmentCode = mAdapter.getItem(position - 1).getOrgCode();
    String dutyScript = mAdapter.getItem(position - 1).getDutyDescript();
    String regStaffId = mAdapter.getItem(position - 1).getRegStaffId();
    String pName = mAdapter.getItem(position - 1).getPname();
    String regDate = mAdapter.getItem(position - 1).getRegDate();//临时数据，下周删除。
    LogUtil.i(TAG, "departmentId==============" + departmentId);
    LogUtil.i(TAG, "customerID==============" + customerID);
    LogUtil.i(TAG, "departmentCode==============" + departmentCode);
    SharedPreferencesUtil.setString(getActivity(), SharedPreferencesUtil.PID, pid);
    SharedPreferencesUtil.setString(getActivity(), SharedPreferencesUtil.MANAGERID, managerId);
    SharedPreferencesUtil.setString(getActivity(), SharedPreferencesUtil.MANAGER_NAME, managerName);

    SharedPreferencesUtil.setString(getActivity(), SharedPreferencesUtil.DIRECTORID, directorId);
    SharedPreferencesUtil.setString(getActivity(), SharedPreferencesUtil.DIRECTOR_NAME,
        directorName);
    SharedPreferencesUtil.setString(getActivity(), SharedPreferencesUtil.DEPARTMENT_NAME,
        departmentName);
    SharedPreferencesUtil.setString(getActivity(), SharedPreferencesUtil.DEPARTMENTID,
        departmentId);
    SharedPreferencesUtil.setString(getActivity(), SharedPreferencesUtil.DATA_STATUS, dataStatus);
    SharedPreferencesUtil.setString(getActivity(), SharedPreferencesUtil.CUSTOMER_ID, customerID);
    SharedPreferencesUtil.setString(getActivity(), SharedPreferencesUtil.CUSTOMER_NAME,
        customerName);
    SharedPreferencesUtil.setString(getActivity(), SharedPreferencesUtil.DEPARTMENT_CODE,
        departmentCode);
    SharedPreferencesUtil.setString(getActivity(), SharedPreferencesUtil.REG_DATA, regDate);
    SharedPreferencesUtil.setString(getActivity(), SharedPreferencesUtil.DUTYSCRIPT, dutyScript);
    SharedPreferencesUtil.setString(getActivity(), SharedPreferencesUtil.PORGNAME, regStaffId);
    SharedPreferencesUtil.setString(getActivity(), SharedPreferencesUtil.PNAME, pName);

    Intent intent = new Intent(getActivity(), CustomerRelationDepartmentDetailActivity.class);
    intent.putExtra(CustomerRelationDepartmentDetailFragment.EXTRA_DEPARTMENT, departMentList);
    startActivityForResult(intent, REQUEST_ALTER);
  }

  /**
   * 处理添加部门按钮的事件
   */
  @Override public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
      case R.id.menu_item_add:
        Intent addDepartmentIntent =
            new Intent(getActivity(), CustomerRelationAddDepartmentActivity.class);
        addDepartmentIntent.putExtra(CustomerRelationAddDepartmentFragment.EXTRA_DEPARTMENT,
            departMentList);
        startActivityForResult(addDepartmentIntent, REQUEST_ADD);
        break;

      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }
    if (requestCode == REQUEST_ADD) {

      volleyPost();
    }
    if (requestCode == REQUEST_ALTER) {

      volleyPost();
    }
    super.onActivityResult(requestCode, resultCode, data);
  }
}
